<?php
// php最大执行时间设置为：半个小时，php原来默认为30秒，爬不完
ini_set('max_execution_time', '0');
include '../vendor/autoload.php';// 使用composer安装后引入目录
use QL\QueryList;
try {
    $cates=[
        9=>1704,
        10=>2199,
        11=>2606,
        12=>2981
    ];

    $rules = array(
        'book_url' => array('a', 'href'),    //获取纯文本格式的标题,并调用回调函数1
    );

    foreach ($cates as $cate=>$total_pages){
        for($i=1;$i<=$total_pages;$i++){
            if($cate ==  9 && $i<= 294){
                continue;
            }
            $cate_url='https://m.gzbpi.com/sort/'.$cate.'_'.$i.'/';
            $data = QueryList::get($cate_url)->rules($rules)->range('.xbk')->query()->getData();
            foreach ($data as $v){
                if(empty($v['book_url'])) continue;
                $url ='https://m.gzbpi.com'.$v['book_url'];
                $sql ="insert into source (`url`,`status`,`create_time`,`update_time`,`from`) values ('".$url."',0,'".date('Y-m-d H:i:s',time())."','".date('Y-m-d H:i:s',time())."',1)".";";
                error_log($sql . PHP_EOL, 3, 'data/notes.csv');
            }
            echo $cate .':'.$i.PHP_EOL;
        }
    }

}catch (Exception $e) {
    $exceptionlog = '文件 ['.$e->getFile().'] 第 '.$e->getLine().' 行出现未处理的异常'.PHP_EOL;
    $exceptionlog .= '代码:'.$e->getCode().PHP_EOL;
    $exceptionlog .= '信息:'.$e->getMessage().PHP_EOL;
    $exceptionlog .= '跟踪信息：'.$e->getTraceAsString();
    //记录错误
    error_log(date('Y-m-d H:i:s') . print_r($exceptionlog, 1) . PHP_EOL, 3, 'logs/'.date('Y-m-d').'_'.'exception.log');
}





?>